/**
 * App通用配置
 */

import { getItem, setItem } from '../../util/storage.js'

const state = {
  index: 0,
  isFrist: true,
  location: [],
  cityLongAndLati: getItem('cityLongAndLati'),
  saveWeatherInfo: {},
  value: getItem('value'),
  userid: getItem('userid')==undefined&&getItem('userid')==null?'e078de53e0504c8c9cf9a03c40a66fe9':getItem('userid'),
  user: getItem('user'),
  livedataloction: getItem('livedataloction'),
  element: getItem('element'),
  username:getItem('username'),
  cancelTokenArr: [] // 取消请求token数组
}
const actions = {
  changeIndex({ commit }, n) {
    commit('INDEX', n)
  },
  setFrist({ commit }, state) {
    commit('ISFRIST', state)
  },

  setLocation({ commit }, state) {
    commit('LOCATION', state)
  },
  setCityLongAndLati({ commit }, state) {
    commit('CITYLONGANDLATI', state)
  },
  setWeatherInfo({ commit }, state) {
    commit('WEATHERINFO', state)
  },
}
const getters = {
  index: (state) => state.index,
  isFrist: (state) => state.isFrist,
  location: (state) => state.location,
  userid:(state) =>state.userid,
  cityLongAndLati: (state) => state.cityLongAndLati,
  saveWeatherInfo: (state) => state.saveWeatherInfo,
  element: (state) => state.element,
}
const mutations = {
  ['INDEX'](state, n) {
    state.index = n
  },
  ['ISFRIST'](state, status) {
    state.isFrist = Boolean(status)
  },
  ['LOCATION'](state, params) {
    state.location = params
    setItem('location', params)
  },
  ['CITYLONGANDLATI'](state, params) {
    state.cityLongAndLati = params
    setItem('cityLongAndLati', params)
  },
  ['WEATHERINFO'](state, params) {
    state.saveWeatherInfo = params
  },
  setUser(state, params) {
    state.user = params
    setItem('user', params)
  },
  setuserid(state, params) {
    state.userid = params
    setItem('userid', params)
  },
  setlivedataloction(state, params) {
    state.livedataloction = params
    setItem('livedataloction', params)
  },
  setValue(state, params) {
    state.value = params
    console.log(params)
    setItem('value', params)
  },
  setelement(state, params) {
    state.value = params

    setItem('element', params)
  },
  setusername(state,params){
    state.username = params
    setItem('username', params)
  },
  pushToken (state, payload) {
    state.cancelTokenArr.push(payload.cancelToken)
  },
  clearToken ({ cancelTokenArr }) {
    cancelTokenArr.forEach(item => {
      item('路由跳转取消请求')
    })
    cancelTokenArr = []
  }

}
export default {
  state,
  actions,
  getters,
  mutations,
}
